/**
 * Created by ebalmes Oct 8, 2009 6:00:57 PM
 * 
 */
package gui.components;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import gui.validators.IValidator;

import javax.swing.JTextField;

public class XField extends JTextField implements FocusListener
{
  IValidator validator;

  public XField(IValidator validator, int columns) {
    super(columns);
    this.validator = validator;
    addFocusListener(this);
  }

  public boolean validateField() {
    return validator.validate(this);
  }

  @Override
  public void focusGained(FocusEvent e) {
    // TODO Auto-generated method stub
  }

  @Override
  public void focusLost(FocusEvent e) {
    if (getText().equals("")) {
      return;
    }
    if (!validateField()) {
      setBackground(Color.RED);
      return;
    } 
    setBackground(Color.WHITE);
  }
}
